var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    categoryStatus: false,
    multiArray: [],
    objectMultiArray: [],
    multiIndex: [],
    oneid: "",
    twoid: "",
    twoname: "",

    categoryIdOne: "",
    categoryIdTwo: "",
    listCategoryOne: [
    ],
    categorySelect: "",
    num: ""
  },
  //品类选择
  bindMultiPickerChange: function (e) {
    console.log("11111", e.detail.value);
    var that = this;
    that.setData({
      "multiIndex[0]": e.detail.value[0],
      "multiIndex[1]": e.detail.value[1],
      categoryStatus: true,
      categoryIdOne: that.data.objectMultiArray[e.detail.value[0]].parid,
      categoryIdTwo: that.data.objectMultiArray[e.detail.value[0]].array[e.detail.value[1]].parid
    })

    console.log("一级品类id", that.data.objectMultiArray[e.detail.value[0]].parid);//一级品类id
    that.setData({
      "oneid": that.data.objectMultiArray[e.detail.value[0]].parid,
    })
    console.log("二级品类id", that.data.objectMultiArray[e.detail.value[0]].array[e.detail.value[1]].parid);//二级品类id
    that.setData({
      "twoid": that.data.objectMultiArray[e.detail.value[0]].array[e.detail.value[1]].parid,
    })
    console.log("二级品类name", that.data.objectMultiArray[e.detail.value[0]].array[e.detail.value[1]].regname);//二级品类name
    that.setData({
      "twoname": that.data.objectMultiArray[e.detail.value[0]].array[e.detail.value[1]].regname,
    })
  },
  bindMultiPickerColumnChange: function (e) {
    console.log("222222", e.detail.value, e.detail.column);
    var that = this;
    switch (e.detail.column) {
      case 0:
        var list = []
        for (var i = 0; i < that.data.objectMultiArray.length; i++) {
          for (var j = 0; j < that.data.objectMultiArray[e.detail.value].array.length; j++) {
            if (that.data.objectMultiArray[i].parid == that.data.objectMultiArray[e.detail.value].array[j].regid) {
              list.push(that.data.objectMultiArray[i].array[j].regname)
            }
          }
        }
        that.setData({
          "multiArray[1]": list,
          "multiIndex[0]": e.detail.value,
          "multiIndex[1]": 0
        })
    }
  },
  bindFormSubmit: function () {
    var that = this;
    wx.request({
      method: "POST",
      url: app.preUrl+'addRisk',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        userId: app.globalData.userId,
        categoryId: that.data.twoid,
        parentId: that.data.oneid,
        categoryName: that.data.twoname,
        warning: that.data.num,
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code == 100) {
          wx.navigateBack({
            delta: 1
          })
        }
        if (res.data.code == 108) {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          })
        }
      },
      fail: function (error) {
        that.setData({ buthiddenNext: true });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.myquery()
  },
  //菜单选择
  categoryList: function (e) {
    var that = this;
    that.setData({//把选中值放入判断值
      categorySelect: e.currentTarget.dataset.select,
    });
  },
  //录入风险数值
  onNumber: function (e) {
    var that = this;
    that.setData({ num: e.detail.value.replace(/\D/g, '') });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
//查询当前用户所有分类
myquery(){
  var that = this
  wx.request({
    method: "POST",
    url: app.preUrl + 'getListCategory',
    dataType: 'json',
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    data: {
      userId: app.globalData.userId,
    },
    success: function (res) {
      console.log(res.data.result)
      var arr1 = []
      var arr2 = []

      var multiArray = []
      var parentsid = []
      for (var i = 0; i < res.data.result.length; i++) {
        var obj = {}
        obj.name = res.data.result[i].name;
        obj.parid = res.data.result[i].categoryId;
        console.log(res.data.result[i].categoryList.length)
        obj.array = []
        for (var k = 0; k < res.data.result[i].categoryList.length; k++) {
          obj.array.push({
            "regid": res.data.result[i].categoryId,
            "parid": res.data.result[i].categoryList[k].categoryId,
            "regname": res.data.result[i].categoryList[k].name
          })
        }


        parentsid.push(obj.parid)
        multiArray.push(obj)
        arr1.push(res.data.result[i].name)
      }
      console.log(multiArray)
      that.setData({ objectMultiArray: multiArray });
      for (var j = 0; j < res.data.result[0].categoryList.length; j++) {
        arr2.push(res.data.result[0].categoryList[j].name)
      }
      var arr = [arr1, arr2]
      that.setData({ multiArray: arr });
    },
    fail: function (error) {
      that.setData({ buthiddenNext: true });
    }
  })
},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.myquery()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})